package com.juitar.base;

/**
 *
 *<pre>
 * C  C#  D  D#  E  F  F#  G  G#  A  A#  B  C
 * 0  1   2  3   4  5  6   7  8   9  10  11 12
 *</pre>
 */

public class Note {

	private int _note;

	private Note(int note) {
		if (note < 0) {
			throw new IllegalArgumentException("Negative note - " + note);
		}
		_note = note % 12;
	}

	final public static int C = 0;
	final public static int Cxx = 1, Db = 1;
	final public static int D = 2;
	final public static int Dxx = 3, Eb = 3;
	final public static int E = 4;
	final public static int F = 5;
	final public static int Fxx = 6, Gb = 6;
	final public static int G = 7;
	final public static int Gxx = 8, Ab = 8;
	final public static int A = 9;
	final public static int Axx = 10, Bb = 10;
	final public static int B = 11;

	static String[] noteTextSharp =
		{ "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
	static String[] noteTextFlat =
		{ "C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B" };

	public static boolean equals(int n1, int n2) {
		//assert n1>=0, n2>=0
		return n1 % 12 == n2 % 12 ? true : false;
	}

	public static String toString(int note) {
		return noteTextFlat[note %= 12];
	}

	public static int toNote(String note) {
		for (int i = 0; i < 12; i++) {
			if (note.compareToIgnoreCase(noteTextSharp[i]) == 0
				|| note.compareToIgnoreCase(noteTextFlat[i]) == 0) {
				return i;
			}
		}
		throw new IllegalArgumentException("Unknown note " + note);
	}

	public int getIntValue() {
		return _note;
	}

	public String toString() {
		return noteTextFlat[_note %= 12];
	}

	final public static Note[] NOTES =
		{
			new Note(C),
			new Note(Db),
			new Note(D),
			new Note(Eb),
			new Note(E),
			new Note(F),
			new Note(Gb),
			new Note(G),
			new Note(Ab),
			new Note(A),
			new Note(Bb),
			new Note(B)};
}
